package com.star.view;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.alibaba.fastjson.JSON;
import com.star.model.MessageModel;
import com.star.model.NewsModel;
import com.star.model.PlModel;
import com.star.model.UserModel;
import com.star.service.MessageService;
import com.star.service.NewsDealWithService;
import com.star.service.PlDealWithService;
import com.star.service.PlService;
import com.star.service.UserService;
import com.star.utils.DateUtils;
import com.star.utils.HtmlContentGet;
import com.star.utils.StrUtils;

@Controller
public class ContentActionView extends MultiActionController {

	@Autowired
	private NewsDealWithService newsDealWithService;

	@Autowired
	private PlService plService;

	@Autowired
	private PlDealWithService plDealWithService;

	@Autowired
	private MessageService messageService;

	@Autowired
	private UserService userService;

	@RequestMapping(value = "/xhInteste.htm", method = RequestMethod.POST)
	public void xhInteste(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("application/json; charset=UTF-8");
		String newsId = request.getParameter("newsId");
		UserModel userModel = (UserModel) request.getSession().getAttribute("userModel");
		try {
			if (userModel != null && StrUtils.isNotEmpty(newsId)) {
				PlModel plModel = plService.queryByNewsId(Integer.parseInt(newsId));
				if (plModel == null) {
					plModel = new PlModel();
					plModel.setNewsId(Integer.parseInt(newsId));
					plModel.setPlrs(0);
					plModel.setXhrs(1);
					plService.insertPl(plModel);
				} else {
					plService.updateXhRs(plModel.getId());
				}
				Map<String, Object> model = new HashMap<String, Object>();
				model.put("flag", "success");
				response.getWriter().write(JSON.toJSONString(model));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "/newspl.htm", method = RequestMethod.POST)
	public void newspl(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("application/json; charset=UTF-8");
		String newsId = request.getParameter("newsId");
		String content = request.getParameter("content");
		UserModel userModel = (UserModel) request.getSession().getAttribute("userModel");
		try {
			boolean flag = plDealWithService.dealCorePl(Integer.parseInt(newsId), content, userModel.getId());
			if (flag) {
				List<MessageModel> messageList = messageService.queryMessageModel(Integer.parseInt(newsId));
				if (messageList != null && messageList.size() > 0) {
					for (MessageModel message : messageList) {
						UserModel userMdel = userService.selectUserById(message.getUid());
						message.setUserName(userMdel.getUserName());
						DateUtils utils = new DateUtils();
						String dateTime = utils.howLong2("Hm", DateUtils.formateDate(new Date()),
								DateUtils.formateDate(message.getGmt_create()));
						message.setDataChange(dateTime);
					}
				}
				response.getWriter()
						.write(JSON.toJSONString(messageList));
			}
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}

	@RequestMapping(value = "/content.htm", method = RequestMethod.GET)
	public ModelAndView content(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> model = new HashMap<String, Object>();
		String url = request.getParameter("url");
		String key = request.getParameter("key");
		String newsId = request.getParameter("newsId");
		HtmlContentGet contentGet = new HtmlContentGet();
		String htmlContent = contentGet.getContent(url);
		if (StrUtils.isNotEmpty(key) && StrUtils.isNotEmpty(newsId)) {
			List<NewsModel> newsList = newsDealWithService.queryListContent(Integer.parseInt(key),
					Integer.parseInt(newsId));
			if (newsList.size() > 0) {
				model.put("newsList", newsList);
			}
		}

		PlModel plModel = plService.queryByNewsId(Integer.parseInt(newsId));
		if (plModel == null) {
			plModel = new PlModel();
			plModel.setNewsId(Integer.parseInt(newsId));
			plModel.setPlrs(0);
			plModel.setXhrs(0);
		}
		List<MessageModel> messageList = messageService.queryMessageModel(Integer.parseInt(newsId));
		if (messageList != null && messageList.size() > 0) {
			for (MessageModel message : messageList) {
				UserModel userMdel = userService.selectUserById(message.getUid());
				message.setUserName(userMdel.getUserName());
				DateUtils utils = new DateUtils();
				String dateTime = utils.howLong2("Hm", DateUtils.formateDate(new Date()),
						DateUtils.formateDate(message.getGmt_create()));
				message.setDataChange(dateTime);
			}

			model.put("messageList", messageList);
		}
		model.put("plModel", plModel);
		model.put("url", url);
		model.put("newsId", newsId);
		model.put("htmlContent", htmlContent);
		return new ModelAndView("/content", model);
	}
}
